import React from 'react'
import PropTypes from 'prop-types'
import { Modal, Form, Input ,Select, message} from 'antd';
import api from "../../../../api/api";
import axios from 'axios';
import fm from "../index";
const FormItem = Form.Item;
const { Option } = Select;
// const { getFieldProps } = this.props.form;
@Form.create()
class Model extends React.Component {

    constructor(props) {
    super(props)
    this.state = { 
      param: {
        code: "",
        fullName: "",
        name: "",
        pcode:"",
        createTime: "",
        orgCode: "",
        thirdFactoryId:"",
        gid:"",
        hrpid:"",
        oldFullName:"",
        factoryId:null
    }
    ,companyId:null};
  }
  // formRef = React.createRef()
  // constructor(props) {
  //   super(props)
  //   this.state = { visible: false };
  // }


  componentDidMount() {
    this.clearData()
    this.props.onRef(this)
    const { setBaseInfo } = this
    // this.handleStartMethod()
  }
  
  // handleStartMethod(){
  //   Parent.handleChange();
  // }

  handleChange=(value)=> {
    this.setState({
      companyId: value
    })
  }

  handleOk = () => {
    // this.props.form.validateFields((error, values) => {
      //   console.info("err=========", error);
      //   console.info("values=========", values);
      //   if (error != null) {
      //     return;
      //   }else{
      //     let { param } = this.state
      //     console.log(values.code)
      //     console.log(values.fullName)
      //     console.log(values.name)
      //     param.code = values.code
      //     param.fullName = values.fullName
      //     param.name = values.name
      //     this.setState({ param }, () => {
      //       this.factoryData()
      //     })
      //   }
    this.props.form.validateFields((error, values) => {
      console.info("err=========", error);
      console.info("values=========", values);
      if (error != null) {
        return;
      }else{
        let { param } = this.state
        console.log(values.code)
        console.log(values.fullName)
        console.log(values.name)
        param.code = values.code
        param.fullName = values.fullName
        param.name = values.name
        this.setState({ param }, () => {
          this.factoryData()
        })
      }
    })
  }
  setBaseInfo = () => {
    console.log("setBaseInfo =-------------------------> 执行了" )
    let data = this.props.data.modelData.gridList
    const { form } = this.props;
    Object.keys(form.getFieldsValue()).forEach(key => {
      const obj = {};
      if (data[key]) {
        obj[key] = data[key] || null;
      }
      form.setFieldsValue(obj);
    });
  };
  //清空form表单数据
  clearData() {
    this.props.form.resetFields();
  }
  //初始化编辑数据
  initEditData = () => {
    this.setBaseInfo()
  }
  // handleOk = () => {
  //   this.formRef.current.validateFields()
  //     .then(values => {
  //       const data = {...values}
  //       console.info(data)
  //       // onOk(data)
  //     })
  //     .catch(errorInfo => {
  //       console.error(errorInfo)
  //     })
  // }

  addCompany(url) {
    const that = this;
    that.state.param.orgCode= localStorage.getItem('orgCode')
    axios({
      method: 'POST',
      url: url,
      data: JSON.stringify(that.state.param),
      headers: {
        "Authorization": localStorage.getItem('token'),
        "Content-Type": "application/json;charset=UTF-8"
      },
    }).then(function (response) {
      console.log("2222222222222222222",response.msg)
      console.log("2222222222222222",response.data.msg)
      if(response.data.msg==="haveCode"){
        message.error({content: "公司编号重复，请修改后再提交"})
      }else if(response.data.msg==="成功"){
        console.log("params.我执行了")
        that.state.companyId = null
        message.success({content: "操作成功"})
        that.handleCancel()
      }else{
        message.error({content: "操作失败"})
      }
      
    })
      .catch(function (error) {
        console.log(error);
      })
  }

  factoryData() {
    let url = ""
    if(this.props.data.modelData.type === 2) {
      url = api.api_URL + "/admin/factory/updateFactoryInfo"
      let { param } = this.state
      param.factoryId = this.props.data.modelData.gridList.factoryId
      param.gid = this.props.data.modelData.gridList.gid
      param.pcode=this.props.data.modelData.gridList.pcode
      param.hrpid = this.props.data.modelData.gridList.hrpid
      param.oldFullName = this.props.data.modelData.gridList.oldFullName

      console.log("this.props.data.gridList.thirdFactoryId - > : "+this.props.data.modelData.gridList.thirdFactoryId)
      param.thirdFactoryId = this.props.data.modelData.gridList.thirdFactoryId
      this.setState({ param }, () => {
        this.addCompany(url)
      })
    }else{
      
      if(this.props.data.modelData.type === 1) {
        let companyId = this.state.companyId===null?-2:this.state.companyId
        url = api.api_URL + "/admin/factory/addFactoryInfo?companyId="+companyId+"&isParent=true"
        this.addCompany(url)
      }
      else{
        let { param } = this.state
        param.factoryId = this.props.data.modelData.gridList.factoryId
        this.setState({ param }, () => {
          url = api.api_URL + "/admin/factory/addFactoryInfo?companyId=-1&isParent=false"
          this.addCompany(url)
        })
      }
    }
  }

  handleCancel = e => {
    console.log(e);
    this.props.handleCancel()
  };

  render() {


    
    // const child   = this.props.data.children
    const { getFieldDecorator } = this.props.form
    // const isParent = 
    // console.log("isParent:",isParent)
    return (
      <div>
        {/* <Modal
          title={this.props.data.title}
          visible={this.props.data.visible}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
        > */}
          <Modal
          title={this.props.data.modelData.title}
          visible={this.props.data.modelData.visible}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
        >
          <Form ref={this.formRef}  name="control-ref"
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 14 }}
            layout="horizontal"
          >
            <FormItem label="公司编码" name="code"  allowClear >
            {getFieldDecorator("code", {
               rules: [{ required: true, message: '公司编码不可缺！' } ,
              ]
            })(
                <Input  disabled={this.props.data.modelData.type=="2" ? true : false}/>
              )}
            </FormItem>
            <FormItem label="公司全称" name="fullName">
            {getFieldDecorator("fullName", {
               rules: [{ required: true, message: '公司全称不可缺' } ,
              ]
            })(
                <Input />
              )}
            </FormItem>
            <FormItem label="公司简称" name="name">
            {getFieldDecorator("name", {
               rules: [{ required: true, message: '公司简称不可缺' } ,
              ]
            })(
                <Input />
              )}
            </FormItem>
            {/*{this.props.data.modelData.isParent ? <FormItem label="公司名称" name="thirdFactoryId">*/}
                            {/*{getFieldDecorator("thirdFactoryId", {*/}
                                {/*rules: [*/}
                                    {/*{*/}
                                        {/*required: true,*/}
                                        {/*message: "请选择公司名称"*/}
                                    {/*}*/}
                                {/*]*/}
                            {/*})(*/}
                                {/*<Select placeholder={"请选择公司"} style={{width: 200}} onChange={this.handleChange} allowClear >*/}
                                    {/*{this.props.data.children}*/}
                                {/*</Select>*/}
                            {/*)}*/}
                        {/*</FormItem> : <div></div>}*/}
          </Form>
        </Modal>
      </div>
    );
  }
}
Model.propTypes = {
  type: PropTypes.string,
  item: PropTypes.object,
  onOk: PropTypes.func,
}
export default Model